<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="width: 100%"
  >
    <el-col :span="20">
      <i class="el-icon-office-building" v-if="isRoot"></i>
      <i class="el-icon-s-data" v-else-if="node.isLeaf"></i>
      <i class="el-icon-remove-outline" v-else-if="node.expanded"></i>
      <i class="el-icon-circle-plus-outline" v-else></i>
      <span class="company-name">{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col :span="12">{{ treeNode.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->

            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit"
                >查看部门</el-dropdown-item
              >
              <el-dropdown-item v-if="!isRoot" command="delete"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from "@/api/departments";
export default {
  name: "dep-tool",
  props: {
    treeNode: {
      type: Object,
      required: true,
    },
    isRoot: {
      type: Boolean,
      default: false,
    },
    node: {
      type: Object,
    },
  },
  methods: {
    handleCommand(command) {
      switch (command) {
        case "add":
          // 添加部门
          this.$emit("addDepts", this.treeNode);
          break;
        case "edit":
          this.$emit('editDepts',this.treeNode)
          break;
        case "delete":
          // 删除部门
          this.$confirm(`你确定要删除${this.treeNode.name}吗？`, "温馨提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
          })
            .then(() => {
              return delDepartment(this.treeNode.id);
            })
            .then((res) => {
              this.$emit("delDepts");
              this.$message.success(this.treeNode.name + "删除成功");
            });
          break;
      }
    },
  },
};
</script>

<style lang='scss' >
.el-col {
  .el-col-12 {
    text-align: right;
  }
}
.el-tree-node__expand-icon {
  color: transparent !important;
}
</style>